<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>layui</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
		<link rel="stylesheet" href="../../css/public.css" media="all">
		<style>
			body {
            background-color: #ffffff;
        }
    </style>
	</head>
	<body>
		<!-- <div class="layuimini-container"> -->
		<!-- <div class="layuimini-main"> -->
		<form class="layui-form" action="">
	<input type="hidden" name="id">
			<div class="layui-form layuimini-form">
				<div class="layui-form-item">
					<label class="layui-form-label required">菜单名称</label>
					<div class="layui-input-block">
						<input type="text" name="menuName" lay-verify="required" lay-reqtext="菜单名称不能为空" placeholder="请输入菜单名称" value=""
						 class="layui-input">
						<tip>菜单名称</tip>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label required">菜单页面路径url</label>
					<div class="layui-input-block">
						<input type="text" name="url" lay-verify="required" lay-reqtext="菜单页面路径不能为空" placeholder="请输入菜单页面路径" value=""
						 class="layui-input">
						<tip>菜单页面路径url</tip>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label ">权限标识</label>
					<div class="layui-input-block">
						<input type="text" name="mCode" placeholder="请输入权限标识代码" value="" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label required">类型</label>
					<div class="layui-input-block">
						<input type="radio" name="mType" value="0" title="目录" >
						<input type="radio" name="mType" value="1" title="菜单" checked="">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">上级菜单</label>
					<div class="layui-input-inline">
						<select name="pid" id="pid" lay-verify="required" lay-search="">

						</select>
					</div>
				</div>
				<div class="layui-form-item layui-form-text">
					<label class="layui-form-label">说明</label>
					<div class="layui-input-block">
						<textarea name="mDescribe" class="layui-textarea" placeholder="请输入备注信息"></textarea>
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
					</div>
				</div>
			</div>

		</form>
		</div>
		</div>


		<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
		<script>
			var baseUrl = "http://localhost:8083/face/api/";
			layui.use(['form'], function() {
				var form = layui.form,
					layer = layui.layer;
				var $ = layui.jquery;

				//监听提交
				form.on('submit(saveBtn)', function(data) {
					// var index = layer.alert(JSON.stringify(data.field), {
					// 	title: '最终的提交信息'
					// }, function() {
					// 	// 关闭弹出层
					// 	layer.close(index);
					// 	var iframeIndex = parent.layer.getFrameIndex(window.name);
					// 	parent.layer.close(iframeIndex);
					// });
					$.ajax({
						url: baseUrl + 'menu/save',
						type: "POST",
						dataType: 'JSON',
						data:data.field,
						success: function(result) {
							console.log(result.data)
							var data = result.data;
					if(result.code == 0){
						layer.alert("保存成功！");
							// 关闭弹出层
							layer.close();
							var iframeIndex = parent.layer.getFrameIndex(window.name);
							parent.layer.close(iframeIndex);
					}
				
						},
						error: function(XMLHttpRequest, textStatus, errorThrown) {
							// 状态码
							console.log(XMLHttpRequest.status);
							// 状态
							console.log(XMLHttpRequest.readyState);
							// 错误信息
							console.log(textStatus);
						}
					});
					return false;
				});
				//重新渲染表单函数
				function renderForm() {
					layui.use('form', function() {
						var form = layui.form; //高版本建议把括号去掉，有的低版本，需要加()
						form.render();
					});
				};
				var htmls = '<option value="">请选择</option>';
				//赋值部分
				$.ajax({
					url: baseUrl + 'menu/admin/list',
					type: "GET",
					async: false,
					dataType: "json",
					success: function(result) {
						console.log(result.data)
						var data = result.data;
						//select赋值
						var ids = [];
						console.log("ids:" + ids);
						for (var x in data) {
							htmls += '<option value="' + data[x].id + '">' + data[x].title + '</option>';
						}
						$("#pid").html(htmls);
						renderForm(); //表单重新渲染，要不然添加完显示不出来新的

					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						// 状态码
						console.log(XMLHttpRequest.status);
						// 状态
						console.log(XMLHttpRequest.readyState);
						// 错误信息
						console.log(textStatus);
					}
				});
				//表单取值
				layui.$('#LAY-component-form-getval').on('click', function() {
					var data = form.val('example');
					alert(JSON.stringify(data));
				});

			});
		</script>
	</body>
</html>
